using UnityEngine;
using FrameworkDesign;

namespace ShootingEditor2D
{
    public class Gun : ShootingEditor2DController
    {
        private Bullet mBullet;
        private GunInfo mGunInfo;
        
        private int mMaxBulletCount;

        private void Awake()
        {
            mBullet = transform.Find("Bullet").GetComponent<Bullet>();
            
            mGunInfo = this.GetSystem<IGunSystem>().CurrentGun;
            
            mMaxBulletCount = this.SendQuery(new MaxBulletCountQuery(mGunInfo.Name.Value));
            
            this.RegisterEvent<OnCurrentGunChanged>(e =>
            {
                mMaxBulletCount = this.SendQuery(new MaxBulletCountQuery(e.Name));
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }
  
        public void Shoot()
        {
            if (mGunInfo.BulletCountInGun.Value > 0 && mGunInfo.State.Value == GunState.Idle)
            {

                var bullet = Instantiate(mBullet.transform, mBullet.transform.position, mBullet.transform.rotation);
                // 统一缩放值
                bullet.transform.localScale = mBullet.transform.lossyScale;
                bullet.gameObject.SetActive(true);
          
                this.SendCommand<ShootCommand>();
            }
        }
        
        public void Reload()
        {
            if (mGunInfo.BulletCountInGun.Value < mMaxBulletCount &&
                mGunInfo.BulletCountOutGun.Value > 0 &&
                mGunInfo.State.Value == GunState.Idle)
            {
                this.SendCommand<ReloadCommand>();
            }
        }

        private void OnDestroy()
        {
            mGunInfo = null;
        }
    }
}